//
//  YaoPinTBVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/9/2.
//  Copyright © 2021 zwb. All rights reserved.
//  药品出价专用

import UIKit

import Eureka
class ZyaYaoPinTBVC: ZyaTenderBaseForm {
  
    var itemSpm :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "商品名*",appearance:appe )
         return v
    }()
    
    var itemTym :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "通用名*",appearance:appe )
         return v
    }()
    var itemCfyhl :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "成分与含量*",appearance:appe )
         return v
    }()
    var itemBzgg :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "包装规格*",appearance:appe )
         return v
    }()
    var itemJjdw :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "计价单位",appearance:appe )
        v.disabled = true
         return v
    }()
    var itemPzwh :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "批准文号*",appearance:appe )
         return v
    }()
    var itemBzq :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
            .phoneInput(true)
         let v = KSFormHItem(title: "保质期(月)",appearance:appe )
         return v
    }()
    var itemSccj :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "生产厂家*",appearance:appe )
         return v
    }()
    var itemGys :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "供应商*",appearance:appe )
         return v
    }()
    var itemCpsm :KSFormHItem = {
        let appe = KSFormHAppear()
            .contentTrailing(rightPP)
            .assistImgStr("tenderBJ")
            .contentHight(contentHHH)
            .contentFont(UIFont.systemFont(ofSize: 17))
         let v = KSFormHItem(title: "产品说明*",appearance:appe )
         return v
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
     
        
       
        
        
        
        
        
        
    }
    
    
   override func configView() {
       footbg.removeAllItem()
            
        if self.depositRemain < self.one.zDepositMoney.doubleWrap {
            footbg.add(itemtip)
        }
    
       itemr.setTempTitle("出价人*")
   
        footbg.add(itemdw).add(itemr).add(itemdh).add(itembzj)
        
        var startPrice = 0.0
        var editable = false
        if doubleTran(self.one.zPrice) > 0 {
            startPrice = doubleTran(self.one.zPrice)
        } else if self.one.zZbPrice.doubleWrap > 0 {
            startPrice = self.one.zZbPrice.doubleWrap
        } else {
            editable = true
        }
        origTbPrice = startPrice

    
        if doubleTran(self.one.zRange) == 0 {// 幅度为0，用手输入
            itemjg_input.setTempTitle("出价价格*")
            footbg.add(itemjg_input).add(itemJjdw)
            itemjg_input.appearance.tailDesc = "元/\(self.one.zUnitNm.unwrap)"
            itemjg_input.updateTailDesc()
     
        } else {
            if startPrice == 0 { // 幅度不为0，但是起始报价为0，用手输入
                itemjg_input.setTempTitle("出价价格*")
                footbg.add(itemjg_input).add(itemJjdw)
                itemjg_input.appearance.tailDesc = "元/\(self.one.zUnitNm.unwrap)"
                itemjg_input.updateTailDesc()
                
            } else { // 幅度不为0，起始报价也不为0，用加减按钮
                var maxV:Double = 1000000
                var minV:Double = 0.0
                if self.one.zType.unwrap == "0" {  // 卖标 的时候  起始价为最小价
                    if startPrice > 0 {
                        minV = Double(startPrice)
                    }
                } else {  // 买标  的时候  起始价为最高价
                    if startPrice > 0 {
                        maxV = Double(startPrice)
                    }
                }
                itemjg.setTempTitle("出价价格*")
                footbg.add(itemjg).add(itemJjdw)
                itemjg.setCountViewMax(maxV)
                itemjg.setCountViewMin(minV)
                itemjg.setFuduNumber(doubleTran(self.one.zRange))
            }
 
        }
            
             
        footbg .add(itemSpm).add(itemTym).add(itemCfyhl).add(itemBzgg).add(itemPzwh).add(itemBzq).add(itemSccj).add(itemGys).add(itemCpsm)
            
            
//                .add(itemstatus)
//                .add(itemcgs)
        
  
        
        footbg.addSection(sectionx)
        footbg.add(submitButton)
        footer.ks_height = footbg.frame.maxY
        self.tableView.tableFooterView = footer
          
        
    }
    override func configAction() {
        super.configAction()
       
        itemSpm.textEndEditClo = {str in
            self.one.zGoodsName = str
            self.configDataSource()
        }
        itemTym.textEndEditClo = {str in
            self.one.zGeneralName = str
            self.configDataSource()
        }
        itemCfyhl.textEndEditClo = {str in
            self.one.zMainElement = str
            self.configDataSource()
        }
        itemBzgg.textEndEditClo = {str in
            self.one.zSpecification = str
            self.configDataSource()
        }
        itemJjdw.textValueChangeClo = {str in
            self.one.zValuationUnit = str
        }
        itemPzwh.textValueChangeClo = {str in
            self.one.zApprovalNum = str
        }
        itemBzq.textValueChangeClo = {str in
            self.one.zExpirationTimes = str
        }
        itemSccj.textValueChangeClo = {str in
            self.one.zManufacturer = str
        }
        itemGys.textValueChangeClo = {str in
            self.one.zSupplier = str
        }
        itemCpsm.textValueChangeClo = {str in
            self.one.zProductInstructions = str
        }
    }
    override func configDataSource() {
        super.configDataSource()
        
        if self.excuteType == .add {
            if one.zSupplier.unwrap == "" {
                one.zSupplier = USERINFO.usrdesc
            }
            if one.zTel.unwrap == "" {
                one.zTel = PersonInfo!.providerTel
            }
            if one.zName.unwrap == "" {
                one.zName = PersonInfo!.faren
            }
            if one.zValuationUnit.unwrap == "" {
                one.zValuationUnit = one.zUnitNm
            }
            
        }
      
        itemSpm.setVal(one.zGoodsName)
        itemTym.setVal(one.zGeneralName)
        itemCfyhl.setVal(one.zMainElement)
        itemBzgg.setVal(one.zSpecification)
        itemJjdw.setVal(one.zValuationUnit)
        itemPzwh.setVal(one.zApprovalNum)
        itemBzq.setVal(one.zExpirationTimes)
        itemSccj.setVal(one.zManufacturer)
        itemGys.setVal(one.zSupplier)
        itemCpsm.setVal(one.zProductInstructions)
 
        
        let spmH = tempSpCountHeight(one.zGoodsName.unwrap ,lineSpacing: 8)
         // 计算出来的高度高于配置的高度 使用计算的高度
        itemSpm.appearance.contentHight(spmH)
        
        let tymH = tempSpCountHeight(one.zGeneralName.unwrap ,lineSpacing: 8)
        itemTym.appearance.contentHight(tymH)
        
        let cfyhlH = tempSpCountHeight(one.zMainElement.unwrap ,lineSpacing: 8)
        itemCfyhl.appearance.contentHight(cfyhlH)
        
        let bzggH = tempSpCountHeight(one.zSpecification.unwrap ,lineSpacing: 8)
        itemBzgg.appearance.contentHight(bzggH)
        
        
        // 重新布局
        if itemr.superview.isSome {
            footbg.reFrame(item: itemr)
            footer.ks_height = footbg.frame.maxY
            self.tableView.tableFooterView = footer
        }

    }
 
    override func noNullPass() -> Bool {
       let result = super.noNullPass()
        
        
        let vali = Validation()
        vali.required(value: one.zName, msg: "出价人不为空")
        vali.required(value: one.zPrice, msg: "出价价格不为空")
        vali.required(value: one.zGoodsName, msg: "商品名称不为空")
        vali.required(value: one.zGeneralName, msg: "通用名不为空")
        vali.required(value: one.zMainElement, msg: "成分含量不为空")
        vali.required(value: one.zSpecification, msg: "规格不为空")
        vali.required(value: one.zValuationUnit, msg: "计价单位不为空")
        vali.required(value: one.zApprovalNum, msg: "批准文号不为空")
        vali.required(value: one.zExpirationTimes, msg: "保质期不为空")
        vali.required(value: one.zManufacturer, msg: "生产厂家不为空")
        vali.required(value: one.zSupplier, msg: "供应商不为空")
        vali.required(value: one.zProductInstructions, msg: "产品说明不为空")



        guard result && vali.validate() else {
            return false
        }
        
        // 前面校验了非空
        if !one.zExpirationTimes!.isPositiveInteger {
            KSToast("保质期只能是整数")
            return false
        }
        if one.zGeneralName!.count > 50 {
            KSToast("通用名不能超过50字")
            return false
        }
        if one.zGoodsName!.count > 50 {
            KSToast("商品名不能超过50字")
            return false
        }
        if one.zSpecification!.count > 200 {
            KSToast("包装规格不能超过200字")
            return false
        }
        
        
        return true
    }
    
  
    
    
}

